﻿using _20_identity框架_01.Models;
using Microsoft.EntityFrameworkCore;

namespace _23_托管服务
{
    public class ScheduledService : BackgroundService
    {
        private readonly IServiceScope _scope;
        private readonly ILogger _logger;
        public ScheduledService(IServiceScopeFactory serviceScopeFactory, ILogger logger = null)
        {
            this._scope = serviceScopeFactory.CreateScope();
            _logger = logger;
        }
        public override void Dispose()
        {
            this._scope.Dispose();
            base.Dispose();
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                var db = _scope.ServiceProvider.GetRequiredService<MyDbContext>();
                while (!stoppingToken.IsCancellationRequested)
                {
                    var c = await db.Users.LongCountAsync();
                    //_logger.LogInformation("11111");
                    Console.WriteLine("123");
                    await Task.Delay(3000);
                    Console.WriteLine("导出成功"+DateTime.Now);
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
